

#include <stdint.h>
#include <string.h>

#include "views.h"

typedef struct 
{
    int32_t idx; 
    int32_t count;
}animate_t;

static uint32_t fsm, fsmMin,fsmMax;

static animate_t    animate; 

//static views_value_t  g_value;


// void views_value( views_value_t* v )
// {
//     memcpy( &g_value, v, sizeof(views_value_t));
// }

// const views_value_t* views_value_get(void)
// {
//     return &g_value;
// }

// void views_value_reset(void)
// {
//     memset( &g_value, 0, sizeof(views_value_t) );
// }
// views_value_t* views_value_get_ptr( void )
// {
//     return &g_value;
// }

uint32_t views_fsm_get(void)
{
    return fsm;
}

void views_set_fsm(uint32_t value)
{
    fsm = value;
}

void views_fsm_reset(void)
{
    fsm = 0;
}

void views_fsm_toggle(void)
{
    fsm = !fsm;
}



void views_fsm_init(uint32_t min, uint32_t max)
{
    fsmMin = min;
    fsmMax = max;
    fsm = fsmMin;
}

uint32_t views_fsm_inc(void)
{
    fsm ++;
    if( fsm > fsmMax )
    {
        fsm = fsmMin;
    }
    return fsm;
}

uint32_t views_fsm_dec(void)
{
    if( fsm <= fsmMin )
    {
        fsm = fsmMax;
    }
    else 
    {
        fsm --;
    }

    return fsm;
}




void views_animate_set_count(int count)
{
    animate.count = count;
    animate.idx = 0;
}
uint32_t views_animate_get_idx(void)
{
    return animate.idx;
}
uint32_t views_animate_get_idx_by_step(void)
{
    animate.idx ++;
    if( animate.idx >= animate.count )
    {
        animate.idx = 0;
    }
    return animate.idx;
}

void views_animate_set_idx(int idx)
{
    animate.idx = idx;
    if( animate.idx >= animate.count )
    {
        animate.idx = animate.count - 1;
    }
}
void views_animate_add_idx(int idx)
{
    animate.idx += idx;
    if( animate.idx >= animate.count )
    {
        animate.idx = 0;
    }
    else if( animate.idx == 0)
    {
        animate.idx = animate.count - 1;
    }
}
